package com.zerosky.crm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zerosky.crm.po.Chance;
import com.zerosky.crm.po.Employee;
import com.zerosky.crm.service.ChanceService;
import com.zerosky.crm.service.ProductService;
import com.zerosky.crm.util.BaseAction;
import com.zerosky.crm.util.PageInfo;

/**
 * 机会相关业务的Controller
 * 
 * @author 钟璟
 *
 */
@Controller
public class ChanceController extends BaseAction {
	private Logger logger = LoggerFactory.getLogger(ChanceController.class); // 日志	

	@Resource(name = "chanceService")
	private ChanceService chanceService;

	@Resource(name = "productService")
	private ProductService productService;

	@RequestMapping("toChanceList.do")
	public String toChanceList(HttpServletRequest request) {
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("status", 0);
			request.setAttribute("productList", productService.getProductList(map));// 产品
			return "WEB-INF/jsp/listChance";
		} catch (Exception e) {
			return "WEB-INF/jsp/error/500";
		}
	}

	/**
	 * 机会列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryChanceList.do")
	@ResponseBody
	public Map<String, Object> queryChanceList(HttpServletRequest request, Integer limit, Integer page,
			String chanceName) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		PageInfo<Chance> pageInfo = new PageInfo<Chance>();
		Employee user = (Employee) request.getSession().getAttribute("loginUser");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (chanceName != null) {
				map.put("chanceName", chanceName);
			}
			map.put("employeeId", user.getEmployeeId());
			pageInfo.setPageNo(page);
			pageInfo.setPageSize(limit);
			pageInfo = chanceService.getPageInfoChance(map, pageInfo);
			json.put("total", pageInfo.getPageCount());
			json.put("data", pageInfo.getList());
			json.put("code", 1);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}
	/**
	 * 添加
	 * 
	 * @param request
	 * @param chance
	 * @return
	 */
	@RequestMapping("/addChance.do")
	@ResponseBody
	public Map<String, Object> addChance(HttpServletRequest request, Chance chance) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {

			Employee emp = (Employee) request.getSession().getAttribute("loginUser");// 获取到员工
			chance.setChanceId(UUID.randomUUID().toString()); // uuid为主键
			chance.setCreateDate(new Date());// 创建时间
			chance.setCreatePeople(emp.getName());// 创建人
			chance.setSection(emp.getDept().getDeptId());// 添加人的部门编号
			chance.setPersonLiable(emp.getEmployeeId());// 负责人工号
			chance.setStatus("0");
			Integer count = chanceService.saveOrUpdate(chance, "新增");
			if (count > 0) {
				json.put("flag", 1);
				json.put("msg", "添加成功!");
			} else {
				json.put("flag", 0);
				json.put("msg", "添加失败!");
			}

		} catch (Exception e) {
			json.put("flag", -1);
			json.put("msg", "系统异常!");
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 
	 * @param request
	 * @param chanceId
	 * @return
	 */
	@RequestMapping("/modifyChanceQuery.do")
	@ResponseBody
	public Map<String, Object> modifyChanceQuery(HttpServletRequest request, String chanceId) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("chanceId", chanceId);

			Chance chance = chanceService.getChanceById(condition);
			if (chance == null) {
				json.put("flag", 0);
				json.put("msg", "数据出现异常,请稍后再试!");
			} else {
				json.put("flag", 1);
				json.put("chance", chance);
			}
		} catch (Exception e) {
			json.put("flag", -1);
			json.put("msg", "操作失败请刷新后重试!");
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param chance
	 * @return
	 */
	@RequestMapping("/modifyChance.do")
	@ResponseBody
	public Map<String, Object> modifyChance(HttpServletRequest request, Chance chance) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Integer count = chanceService.saveOrUpdate(chance, "修改");
			if (count > 0) {
				json.put("flag", 1);
				json.put("msg", "修改成功!");
			} else {
				json.put("flag", 0);
				json.put("msg", "修改失败!");
			}

		} catch (Exception e) {
			json.put("flag", -1);
			json.put("msg", "系统异常!");
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 删除机会
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteChance.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteChance(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<>();
		try {
			Integer flag = chanceService.deleteChance(request.getParameter("chanceId"));
			if (flag > 0) {
				map.put("msg", "删除成功");
				map.put("code", "success");
			} else {
				map.put("msg", "删除失败，数据异常");
				map.put("code", "error");
			}
		} catch (Exception e) {
			map.put("msg", "删除失败，请稍后再试");
			map.put("code", "error");
			logger.error("获取数据失败，原因：" + e);
		}
		return map;
	}

}
